﻿using System;
using System.Text;
using System.Threading.Tasks;

namespace ZK.Common
{
    public static class Extension
    {
        private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        public static string GetAllExceptionMessages(this Exception ex)
        {
            var msg = new StringBuilder();
            msg.Append(ex.Message);

            if (ex.InnerException != null)
            {
                msg.Append(", ");
                msg.Append(GetAllExceptionMessages(ex.InnerException));
            }

            return msg.ToString();
        }

        public static string ToFormattedString(this Exception ex)
        {
            return $"{nameof(Exception)}, {ex.GetAllExceptionMessages()}";
        }

        public static string ToFormattedString(this DateTime dateTime)
        {
            return dateTime.ToString(DateTimeFormat);
        }

        //public  static async Task<bool> RetryWhenFaild(this Task<bool> task, int maxRetryCount)
        //{
        //    var retryCount = 0;
        //    bool result;
        //    do
        //    {
        //        result =  task.Result;
        //        retryCount++;
        //    }
        //    while (!result && retryCount < maxRetryCount);
        //    return result;
        //}

        public static string ToCamelCase(this string str)
        {
            var firstCharacter = str.Substring(0, 1);
            var remainingCharacters = str.Substring(1);
            return firstCharacter.ToLower() + remainingCharacters;
        }
    }
}